package zhh.tangbao.system.result;

import java.io.Serializable;

/**
 * @author 粥灰灰
 * @created 2022/8/20
 * @version 1.0
 * @Description 返回内容模板
 */
public class Result implements Serializable {

    private static final long serialVersionUID = 1L;

    /** 返回参数的状态码，并非请求状态码，一切都是基于http请求成功的情况下进行的 */
    private Integer code;

    private String msg;

    private Object data;

    public Result setCode(Integer code) {
        this.code = code;
        return this;
    }

    public Result setMsg(String msg) {
        this.msg = msg;
        return this;
    }


    public Result put(Object data) {
        this.data = data;
        return this;
    }

    public static Result success() {
        Result r = new Result();
        r.setCode(200);
        r.setMsg("操作成功");
        return r;
    }

    public static Result successOrFail(Integer count) {
        if (count <= 0) {
            return fail();
        } else {
            return success();
        }
    }

    public static Result fail() {
        Result r = new Result();
        r.setCode(500);
        r.setMsg("操作失败");
        return r;
    }

    public static Result needReEntry() {
        Result r = new Result();
        r.setCode(5001);
        r.setMsg("发生异常，需要重新登录");
        return r;
    }

    public static Result needReEntry(String msg) {
        Result r = new Result();
        r.setCode(5001);
        r.setMsg(msg);
        return r;
    }

    @Override
    public String toString() {
        return "Result{" +
                "code=" + code +
                ", message='" + msg + '\'' +
                ", data=" + data +
                '}';
    }

    public Integer getCode() {
        return code;
    }

    public String getMsg() {
        return msg;
    }

    public Object getData() {
        return data;
    }
}
